﻿(function () {
    RegisterSerializeObject();
}());

/*===注册类声明类型Start===*/
var Class = {
    create: function ()              // create Function  
    {
        return function () {
            this.initialize.apply(this, arguments);
        }
    }
};
/*===注册类声明类型End===*/

/*===表单提交实体Start===*/
var FormEntity = Class.create();
FormEntity.prototype =
    {
        //表单选择器
        selector: "",
        //表单提交地址
        url: "",
        //表单参数
        data: {},
        //表单数据实体
        formData: {},
        //表单提交方式
        type: "POST",
        //内容类型
        contentType: "application/x-www-form-urlencoded",
        //默认返回数据为json格式
        dataType: "json",
        //是否异步请求
        async: true,
        //是否缓存
        cache: false,
        //表单提交前事件
        submiting: function () {
            return true;
        },
        //表单提交失败事件
        submitFailed: function () {
        },
        //表单处理内容，默认Ajax表单提交
        handle: function () {
            var $this = this;
            //判断是否应用插件jquery.form.js，该插件支持Ajax文件上传
            if ($($this.selector).ajaxSubmit) {
                $($this.selector).ajaxSubmit({
                    cache: $this.cache,
                    type: $this.type,
                    url: $this.url,
                    data: $this.data,
                    dataType: $this.dataType,
                    contentType: $this.contentType,
                    async: $this.async,
                    success: function (InvokeResult) {
                        $this.submitSuccess(InvokeResult);
                        $this.submited();
                        if ($this.isClearData) {
                            $this.clearFormData();
                        }
                    }
                });
            } else {
                $.ajax({
                    cache: this.cache,
                    type: this.type,
                    url: this.url,
                    data: this.data,
                    dataType: this.dataType,
                    contentType: this.contentType,
                    async: this.async,
                    success: function (InvokeResult) {
                        $this.submitSuccess(InvokeResult);
                        $this.submited();
                        if ($this.isClearData) {
                            $this.clearFormData();
                        }
                    }
                });
            }
        },
        //表单提交
        submit: function () {
            if (!this.submiting()) {
                this.submitFailed();
                return;
            }
            this.handle();
        },
        //表单提交成功事件
        submitSuccess: function (invokeresult) {
        },
        //表单提交完成事件
        submited: function () {
        },
        //是否清楚表单数据
        isClearData: true,
        //清除表单数据
        clearFormData: function () {
            $(this.selector).find(':input').each(
                function () {
                    switch (this.type) {
                        case 'passsword':
                        case 'select-multiple':
                        case 'select-one':
                        case 'text':
                        case 'textarea':
                            $(this).val('');
                            break;
                        case 'checkbox':
                        case 'radio':
                            this.checked = false;
                    }
                }
            );
        },
        //构造函数
        initialize: function (selector) {
            this.selector = selector;
            this.data = $(this.selector).serialize();
            this.formData = $(this.selector).serializeObject()
        }
    };
/*===表单提交实体End===*/

/*===Ajax请求实体Start===*/
var AjaxEntity = Class.create();
AjaxEntity.prototype =
    {
        //请求地址
        url: "",
        //请求参数
        data: {},
        //请求方式
        type: "POST",
        //默认返回数据为json格式
        dataType: "json",
        //内容类型
        contentType: "application/x-www-form-urlencoded",
        //是否异步请求
        async: true,
        //是否缓存
        cache: false,
        //请求前事件
        executing: function () {
            return true;
        },
        //请求失败事件
        executeFailed: function () {
        },
        //请求事件
        execute: function () {
            if (!this.executing()) {
                this.executeFailed();
                return;
            }
            var $this = this;
            $.ajax({
                cache: this.cache,
                type: this.type,
                url: this.url,
                data: this.data,
                dataType: this.dataType,
                contentType: this.contentType,
                async: this.async,
                success: function (InvokeResult) {
                    $this.executeSuccess(InvokeResult);
                    $this.executed();
                }
            });
        },
        //请求成功事件
        executeSuccess: function (invokeresult) {
        },
        //请求完成事件
        executed: function () {
        },
        //构造函数
        initialize: function () {
        }
    };
/*===Ajax请求实体End===*/